﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using KCMS.BusinessLogic.Models;

namespace KCMS.BusinessLogic.Engine
{
    public class CartService : ICartService
    {
        private IProductService productService = ObjectFactory.GetInstance<IProductService>();
        private IProductImageService productImageService = ObjectFactory.GetInstance<IProductImageService>();
        private ISizeService sizeService = ObjectFactory.GetInstance<ISizeService>();
        public CartItem BuildFullModel(CartItem item)
        {
            item.Product = productService.getById(item.productId);
            item.ProductImage = productImageService.getById(item.productImageId);
            item.Size = sizeService.getById(item.sizeId);
            return item;
        }


        public IEnumerable<CartItem> BuildFullModel(IEnumerable<CartItem> items)
        {
            if (items != null)
            {
                items.ToList().ForEach(delegate(CartItem item)
                {
                    item = BuildFullModel(item);
                });
            }
            return items;
        }
        public IEnumerable<OrderItem> ConvertToOrderItems(IEnumerable<CartItem> items, Order order)
        {
            IEnumerable<OrderItem> orderItems = (items.Select(c =>
                new OrderItem
                {
                    OrderId = order.Id,
                    ProductId = c.productId,
                    ProductImageId = c.productImageId,
                    SizeId = c.sizeId,
                    Quantity = c.quantity
                })).ToList();
            return orderItems;
        }

    }
}
